/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:ces.org.course.listener</p>
 * <p>File:FileListenerFactory.java</p>
 * <p>类更新历史信息</p>
 * @todo chanlong(陈龙) 创建于 May 17, 2019 3:55:52 PM
 */
package org.cesgroup.course.api.watch;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.cesgroup.course.api.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date May 17, 2019 3:55:52 PM
 * @version 1.0.2019
 */
@Component
public class FileListenerFactory {
	
	public FileAlterationMonitor monitor() {
		// 创建过滤器
        IOFileFilter directories = FileFilterUtils.and(FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE);
        IOFileFilter files = FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".xml"));
        IOFileFilter filter = FileFilterUtils.or(directories, files);
 
        // 装配过滤器
        FileAlterationObserver observer = new FileAlterationObserver(new File(monitor), filter);
 
        // 向监听者添加监听器，并注入业务服务
        observer.addListener(FileListener.builder().service(service).build());
 
        // 返回监听者
        return new FileAlterationMonitor(interval, observer);
	}
	
	// 设置监听路径
	@Value("${ces.dict.watch}")
	private String monitor;
	
	// 注入字典服务
	@Autowired
	private DictionaryService service;

	// 设置轮询间隔
    private final long interval = TimeUnit.SECONDS.toMillis(1);
}
